﻿Public Class Resultado_Dual
    Dim Vector() As String
    Dim valXlbl As String = ""
    Dim valorZ As Single = 0
    Dim valMlbl As String = ""
    Dim total As Double = 0
    Public Sub New(ByVal arreglo As String(,), ByVal variable As Integer, ByVal tipo As String, ByVal restricciones As Integer, ByVal artificiales1 As Integer, ByVal holguras1 As Integer)

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Dim nuevoF1 As Integer = 3 + holguras1 + artificiales1 + restricciones
        Dim nuevoF2 As Integer = variable
        ReDim Vector(0 To nuevoF2)
        Tabla.ColumnCount = nuevoF1
        Tabla.RowCount = nuevoF2 + 1
        GenerarTitulos((nuevoF1 - artificiales1), artificiales1)

        For n As Integer = 0 To (nuevoF2 + 1) - 1

            For m As Integer = 0 To nuevoF1 - 1
                Tabla(m, n).Value = arreglo(n, m)
            Next
        Next
        AsignarEtiquetas((nuevoF2 + 1), tipo)
        Determinar_multiple(nuevoF2, nuevoF1)
    End Sub
    Public Sub Determinar_multiple(ByVal filas As Integer, ByVal columna As Integer)
        For k As Integer = 0 To filas - 1
            Vector(k) = (Tabla(1, k).Value)
        Next
        For i As Integer = 3 To (columna) - 1
            If Tabla(i, filas).Value.ToString() = "0.0" And Buscar(Tabla.Columns(i).Name, Vector) = False Then
                MessageBox.Show("Resultado de solucion óptima multiple", "Aviso")
            End If
        Next
    End Sub
    Public Function Buscar(ByVal nombrecolumna As String, ByVal Vector As String()) As Boolean
        Dim resultado As Boolean = False
        For h As Integer = 0 To Vector.Length - 1

            If Vector(h) = nombrecolumna Then

                resultado = True

            End If

        Next
        Return resultado

    End Function
    Public Sub GenerarTitulos(ByVal nuevo As Integer, ByVal artificiales1 As Integer)

        For i As Integer = 3 To nuevo - 1

            Tabla.Columns(i).Name = "X" & Convert.ToString(i - 2)

        Next
        For i As Integer = nuevo To (nuevo + artificiales1) - 1

            Tabla.Columns(i).Name = "L" & Convert.ToString(i - nuevo)

        Next

        Tabla.Columns(2).Name = "Bi"
        Tabla.Columns(1).Name = "Xk"
        Tabla.Columns(0).Name = "Ck"
    End Sub
    Public Sub AsignarEtiquetas(ByVal nuevoF2 As Integer, ByVal tipo As String)
        For p As Integer = 0 To (nuevoF2 - 1) - 1

            valXlbl = valXlbl & "  " & (Tabla(1, p).Value & " = " & Convert.ToString(Aproximar(Tabla(2, p).Value, 2)) & "  ") 'Aproximar al centecimo
            valorZ = valorZ + Convert.ToDouble(Tabla(0, p).Value) * Convert.ToDouble(Tabla(2, p).Value)
        Next

        valX.Text = valXlbl
        lblZ.Text = "Valor de Z : " & valorZ.ToString

    End Sub
    Private Function Aproximar(ByVal numero As String, ByVal decimales As Integer) As Double
        Dim potencia As Double = Math.Pow(10, decimales)
        Dim retorno As Double = CInt((Convert.ToDouble(numero) * potencia) / potencia * 1.0)
        Return retorno

    End Function
End Class